import { defineStore } from "pinia";
import { getToken, setToken } from "../utils/auth";
// import { getuserInfo } from "@/api/user";
import { getSetting_api } from "@/api/public";

export const useUserStore = new defineStore({
  id: "user",
  state: () => {
    return {
      userInfo: uni.getStorageSync("userInfo") || {},
      setting: {},
      token: uni.getStorageSync("token") || "",
      temToken: null,
      time: false,
    };
  },

  getters: {
    isLogin: (state) => !!state.token,
  },
  actions: {
    async getUser(data) {
      // const data = await getuserInfo();
      this.userInfo = data;
      uni.setStorageSync("userInfo", data);
    },
    login(token, expire = "") {
      this.token = token;
      uni.setStorageSync("token", token);
      // setToken(token, expire);
      // this.getUser();
    },
    async logout() {
      // cache.remove(TOKEN_KEY);
      this.token = "";
      await uni.$u.sleep(500);
      uni.hideLoading();
      uni.clearStorageSync();
      this.userInfo = {};
      // uni.$u.route({
      //   url: "/pages/login/login",
      //   type: "reLaunch",
      // });
    },
    async getSettingInfo() {
      const data = await getSetting_api();
      this.setting = data;
    },
  },
});
